package com.diudiutechnology.www.yadiudiu.yadiudiu.mine.MyAdapter;

import android.content.Context;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.diudiutechnology.www.yadiudiu.yadiudiu.mine.MyActivity.WaitingActivity;
import com.diudiutechnology.www.yadiudiu.yadiudiu.model.WaitingModel;
import com.diudiutechnology.www.yadiudiu.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/8/31.
 */

public class WaitingActivityAdapter extends BaseAdapter {

    private Context mContext;
    List<WaitingModel> model= new ArrayList<>();
    String judgment="0";
    String merchanttype="";
    public WaitingActivityAdapter(Context context, String judgment,String merchanttype) {
        this.mContext = context;
        this.judgment=judgment;
        this.merchanttype=merchanttype;
    }

    @Override
    public int getCount() {
        return model.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }
    public void addItem( List<WaitingModel> model){

        this.model=model;
        notifyDataSetChanged();
    }
    public void emptying(){
        model.clear();
        notifyDataSetChanged();
    }
    public int number(){
        return   this.model.size();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(
                    R.layout.item_waiting, null);
            holder.item_money = (TextView) convertView
                    .findViewById(R.id.item_money);
            holder.item_state = (TextView) convertView
                    .findViewById(R.id.item_state);

            holder.item_order_number = (TextView) convertView
                    .findViewById(R.id.item_order_number);
            holder.item_order = (TextView) convertView
                    .findViewById(R.id.item_order);
            holder.item_quantity = (TextView) convertView
                    .findViewById(R.id.item_quantity);
            holder.item_articles = (TextView) convertView
                    .findViewById(R.id.item_articles);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        final WaitingModel waitingModel = model.get(position);


        if (judgment.equals("0")){//待接单
            holder.item_state.setVisibility(View.GONE);
            holder.item_order.setVisibility(View.VISIBLE);
             }else if (judgment.equals("1")){//接单
            holder.item_order.setVisibility(View.INVISIBLE);
            if (merchanttype.equals("1")) {//买房商家

            } else if (merchanttype.equals("2")) {//购车商家

            } else if (merchanttype.equals("3")) {//干洗商家
                if (waitingModel.getOrderstate().equals("4")){
                    holder.item_state.setText(Html.fromHtml("<font color='#FD1142'>等待骑手接单</font>"));
                    holder.item_state.setVisibility(View.VISIBLE);
                    holder.item_order.setVisibility(View.INVISIBLE);
                }else if(waitingModel.getOrderstate().equals("5")){
                    holder.item_state.setText(Html.fromHtml("<font color='#FD1142'>等待骑手取件</font>"));
                    holder.item_state.setVisibility(View.VISIBLE);
                    holder.item_order.setVisibility(View.INVISIBLE);
                }else if(waitingModel.getOrderstate().equals("6")){
                    holder.item_state.setText(Html.fromHtml("<font color='#FD1142'>等待骑手送达</font>"));
                    holder.item_state.setVisibility(View.VISIBLE);
                    holder.item_order.setVisibility(View.INVISIBLE);
                }else if(waitingModel.getOrderstate().equals("20")){
                    holder.item_state.setText(Html.fromHtml("<font color='#FD1142'>骑手已送达</font>"));
                    holder.item_state.setVisibility(View.VISIBLE);
                    holder.item_order.setVisibility(View.VISIBLE);
                    holder.item_order.setText("开始洗衣");
                }else if(waitingModel.getOrderstate().equals("7")){
                    holder.item_state.setText(Html.fromHtml("<font color='#FD1142'>正在洗衣</font>"));
                    holder.item_state.setVisibility(View.VISIBLE);
                    holder.item_order.setVisibility(View.VISIBLE);
                    holder.item_order.setText("洗衣完成");
                }else if(waitingModel.getOrderstate().equals("8")){
                    holder.item_state.setText(Html.fromHtml("<font color='#FD1142'>等待骑手接单</font>"));
                    holder.item_state.setVisibility(View.VISIBLE);
                    holder.item_order.setVisibility(View.INVISIBLE);
                }else if(waitingModel.getOrderstate().equals("9")){
                    holder.item_state.setText(Html.fromHtml("<font color='#FD1142'>等待骑手取件</font>"));
                    holder.item_state.setVisibility(View.VISIBLE);
                    holder.item_order.setVisibility(View.INVISIBLE);
                }else if(waitingModel.getOrderstate().equals("10")){
                    holder.item_state.setText(Html.fromHtml("<font color='#FD1142'>骑手配送中</font>"));
                    holder.item_state.setVisibility(View.VISIBLE);
                    holder.item_order.setVisibility(View.INVISIBLE);
                }else if(waitingModel.getOrderstate().equals("11")){
                    holder.item_state.setText(Html.fromHtml("<font color='#FD1142'>骑手配送完成</font>"));
                    holder.item_state.setVisibility(View.VISIBLE);
                    holder.item_order.setVisibility(View.VISIBLE);
                    holder.item_order.setText("确认完成");
                }else if(waitingModel.getOrderstate().equals("12")){
//                holder.item_state.setVisibility(View.VISIBLE);
//                holder.item_order.setVisibility(View.INVISIBLE);
//                holder.item_state.setText("订单已完成");
                    holder.item_state.setText(Html.fromHtml("<font color='#FD1142'>骑手配送完成</font>"));
                    holder.item_state.setVisibility(View.VISIBLE);
                    holder.item_order.setVisibility(View.VISIBLE);
                    holder.item_order.setText("确认完成");
                }
            } else if (merchanttype.equals("4")) {//开锁商家

            } else if (merchanttype.equals("5")) {//果蔬商家
                if (waitingModel.getOrderstate().equals("4")){
                    holder.item_state.setText(Html.fromHtml("<font color='#FD1142'>等待骑手接单</font>"));
                    holder.item_state.setVisibility(View.VISIBLE);
                    holder.item_order.setVisibility(View.INVISIBLE);
                }else if(waitingModel.getOrderstate().equals("5")){
                    holder.item_state.setText(Html.fromHtml("<font color='#FD1142'>等待骑手取件</font>"));
                    holder.item_state.setVisibility(View.VISIBLE);
                    holder.item_order.setVisibility(View.VISIBLE);
                    holder.item_order.setText("确认取件");
                }else if(waitingModel.getOrderstate().equals("6")){
                    holder.item_state.setText(Html.fromHtml("<font color='#FD1142'>等待骑手送达</font>"));
                    holder.item_state.setVisibility(View.VISIBLE);
                    holder.item_order.setVisibility(View.INVISIBLE);
                }else if(waitingModel.getOrderstate().equals("7")){
                    holder.item_state.setText(Html.fromHtml("<font color='#FD1142'>配送完成</font>"));
                    holder.item_state.setVisibility(View.VISIBLE);
                    holder.item_order.setVisibility(View.VISIBLE);
                    holder.item_order.setText("确认完成");
                }else if(waitingModel.getOrderstate().equals("8")){
                    holder.item_state.setText(Html.fromHtml("<font color='#FD1142'>配送完成</font>"));
                    holder.item_state.setVisibility(View.VISIBLE);
                    holder.item_order.setVisibility(View.VISIBLE);
                    holder.item_order.setText("确认完成");
                }
            } else if (merchanttype.equals("6")) {//苦力商家

            } else if (merchanttype.equals("7")) {//快递商家

            } else if (merchanttype.equals("8")) {//家政商家
                if (waitingModel.getOrderstate().equals("3")){
                    holder.item_state.setText(Html.fromHtml("<font color='#FD1142'>正在服务中 </font>"));
                    holder.item_state.setVisibility(View.VISIBLE);
                    holder.item_order.setVisibility(View.VISIBLE);
                    holder.item_order.setText("确认完成");
                }else if(waitingModel.getOrderstate().equals("4")){
                    holder.item_state.setText(Html.fromHtml("<font color='#FD1142'>正在服务中</font>"));
                    holder.item_state.setVisibility(View.VISIBLE);
                    holder.item_order.setVisibility(View.VISIBLE);
                    holder.item_order.setText("确认完成");
                }
            } else if (merchanttype.equals("9")) {//洗车商家

            }
        }else if (judgment.equals("2")){//完成
            holder.item_state.setVisibility(View.VISIBLE);
            holder.item_order.setVisibility(View.INVISIBLE);
            holder.item_state.setText("订单已完成");
        }

        if (merchanttype.equals("1")||merchanttype.equals("2")||merchanttype.equals("4")) {//买房商家 购车商家 开锁商家
            holder.item_order_number.setText("订单号："+waitingModel.getOrdernumber());
            holder.item_quantity.setVisibility(View.GONE);
            holder.item_articles.setText(waitingModel.getOrdername());
            holder.item_money.setVisibility(View.GONE);
            holder.item_money.setText(Html.fromHtml("¥: <font color='#FD1142'>"+waitingModel.getOrdermoney()+"元</font>"));
        }else if (merchanttype.equals("3")||merchanttype.equals("5")) {//果蔬商家
            holder.item_order_number.setText("订单号："+waitingModel.getOrdernumber());
            holder.item_quantity.setText("数量：x"+waitingModel.getQuantity());
            holder.item_articles.setText(waitingModel.getProductname());
            holder.item_money.setText(Html.fromHtml("¥: <font color='#FD1142'>"+waitingModel.getUnitprice()+"元</font>"));
        } else if (merchanttype.equals("6")) {//苦力商家
            holder.item_order_number.setText("订单号："+waitingModel.getOrdernumber());
            holder.item_quantity.setVisibility(View.GONE);
            holder.item_articles.setText(waitingModel.getOrdername());
            holder.item_money.setVisibility(View.GONE);
            holder.item_money.setText(Html.fromHtml("¥: <font color='#FD1142'>"+waitingModel.getOrdermoney()+"元</font>"));
        } else if (merchanttype.equals("7")) {//快递商家

        } else if (merchanttype.equals("8")) {//家政商家
            holder.item_order_number.setText("订单号："+waitingModel.getOrdernumber());
            holder.item_quantity.setVisibility(View.GONE);
            holder.item_articles.setText(waitingModel.getOrdername());
            holder.item_money.setText(Html.fromHtml("¥: <font color='#FD1142'>"+waitingModel.getOrdermoney()+waitingModel.getServicetime()+"</font>"));
        } else if (merchanttype.equals("9")) {//洗车商家
            holder.item_order_number.setText("订单号："+waitingModel.getOrdernumber());
            holder.item_quantity.setVisibility(View.GONE);
            holder.item_articles.setText(waitingModel.getOrdername());
            holder.item_money.setText(Html.fromHtml("¥: <font color='#FD1142'>"+waitingModel.getOrdermoney()+"元</font>"));
        }



        holder.item_order.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (judgment.equals("0")){//待接单
                ((WaitingActivity)mContext).processReceipt(waitingModel.getId());
                }else if (judgment.equals("1")){//已接单
                    if (merchanttype.equals("1")) {//买房商家

                    } else if (merchanttype.equals("2")) {//购车商家

                    } else if (merchanttype.equals("3")) {//干洗商家
                        if (waitingModel.getOrderstate().equals("20")){	//7、正在洗衣
                            ((WaitingActivity)mContext).processoperation(waitingModel.getId(),"7");
                        }else   if (waitingModel.getOrderstate().equals("7")){//8、洗衣完成
                            ((WaitingActivity)mContext).processoperation(waitingModel.getId(),"8");
                        }else if(waitingModel.getOrderstate().equals("11")||waitingModel.getOrderstate().equals("12")){//8、干洗配送完成
                            ((WaitingActivity)mContext).processoperation(waitingModel.getId(),"11");
                        }
                    } else if (merchanttype.equals("4")) {//开锁商家

                    } else if (merchanttype.equals("5")) {//果蔬商家
                        if (waitingModel.getOrderstate().equals("5")){	//确认取件
                            ((WaitingActivity)mContext).processoperation(waitingModel.getId(),"5");
                        }else   if (waitingModel.getOrderstate().equals("7")){//确认完成
                            ((WaitingActivity)mContext).processoperation(waitingModel.getId(),"7");
                        }
                    } else if (merchanttype.equals("6")) {//苦力商家

                    } else if (merchanttype.equals("7")) {//快递商家

                    } else if (merchanttype.equals("8")) {//家政商家
                        if (waitingModel.getOrderstate().equals("3")){
                            ((WaitingActivity)mContext).processoperation(waitingModel.getId(),"3");
                        }
                    } else if (merchanttype.equals("9")) {//洗车商家

                    }



                }
            }
        });
        return convertView;
    }

    class ViewHolder {
        TextView item_money;
        TextView item_state;
        TextView item_order;
        TextView item_order_number;
        TextView item_quantity;
        TextView item_articles;


    }
}

